//
//  MenuView.m
//  NewDaliy
//
//  Created by xuPeng on 2017/8/25.
//  Copyright © 2017年 陈旭鹏. All rights reserved.
//

#import "MenuView.h"
#import "Header.h"
#import "Masonry.h"
#import <pop/POP.h>

#define KHeaderViewH 200
@interface MenuView ()<UITableViewDelegate,UITableViewDataSource>

// 模糊效果层
@property (nonatomic, strong) UIVisualEffectView *bluEffectView;
/** 上半部分设置按钮的父view*/
@property (nonatomic, strong) UIView *headerView;
/** 下半部分菜单按钮的父view*/
@property (nonatomic, strong) UIView *footerView;

// TableView
@property (nonatomic, strong) UITableView *menuTableView;

// 数据
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) NSArray *titleArray;
@end

@implementation MenuView
- (void)hideNewsClassificationView{}

- (void)hideNewsClassificationViewAnimation{}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self  = [super initWithFrame:frame]) {
        UIBlurEffect *bluEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        self.bluEffectView = [[UIVisualEffectView alloc] initWithEffect:bluEffect];
        _bluEffectView.frame = self.frame;

        [self addSubview:self.bluEffectView];
        [self addSubview:self.headerView];
        [self addSubview:self.footerView];
        self.menuTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 190, PH - KHeaderViewH - 80) style:UITableViewStylePlain];
        _menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _menuTableView.backgroundColor = [UIColor clearColor];
        _menuTableView.delegate = self;
        _menuTableView.dataSource = self;
        [self.footerView addSubview:self.menuTableView];
        [self addHeaderViewBtnAndLabel];
    }
    return self;
}

//- (UIVisualEffectView *)bluEffectView{
//    if (!self.bluEffectView) {
//        UIBlurEffect *bluEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
//        _bluEffectView = [[UIVisualEffectView alloc] initWithEffect:bluEffect];
//        _bluEffectView.frame = self.frame;
//    }
//    return _bluEffectView;
//}

- (NSArray *)imageArray{
    if (!_imageArray) {
        _imageArray = @[
                        @"menu_about",
                        @"menu_category",
                        @"menu_column",
                        @"menu_lab",
                        @"menu_noti",
                        @"menu_user",
                        @"menu_home"
                        ];
    }
    return _imageArray;
}

- (NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = @[
                        @"关于我们",
                        @"新闻分类",
                        @"栏目中心",
                        @"好奇心研究所",
                        @"我的消息",
                        @"个人中心",
                        @"首页"
                        ];
    }
    return _titleArray;
}
/// 弹出headerView和footerView
- (void)popupMenuViewAnimation {
    
    //显示JFMenuView
    [self setHidden:NO];
    
    //加判断，防止连击悬浮按钮时出现界面逻辑交互混乱的情况（下同）
    if (-KHeaderViewH == self.headerView.layer.frame.origin.y) {
        [self popAnimationWithView:self.headerView Offset:KHeaderViewH speed:15];
    }
    if (PH == self.footerView.layer.frame.origin.y) {
        [self popAnimationWithView:self.footerView Offset:-(PH - KHeaderViewH) speed:15];
    }
}

/** pop动画
 *  POPPropertyAnimation    动画属性
 *  view                    动画对象
 *  offset                  偏移量
 *  speed                   动画速度
 */
- (void)popAnimationWithView:(UIView *)view Offset:(CGFloat)offset speed:(CGFloat)speed {
    POPSpringAnimation *popSpring = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    popSpring.toValue = @(view.center.y + offset);
    popSpring.beginTime = CACurrentMediaTime();
    popSpring.springBounciness = 11.0f;
    popSpring.springSpeed = speed;
    [view pop_addAnimation:popSpring forKey:@"positionY"];
}

- (void)hideMenuViewAnimation {
    [UIView animateWithDuration:0.1
                     animations:^{
                         [self headerViewOffsetY:-KHeaderViewH];
                         [self footerViewOffsetY:PH];
                     } completion:^(BOOL finished) {
                         //隐藏JFMenuView
                         [self setHidden:YES];
                     }];
}

/// 改变headerView的Y值
- (void)headerViewOffsetY:(CGFloat)offsetY {
    CGRect headerTempFrame = self.headerView.frame;
    headerTempFrame.origin.y = offsetY;
    self.headerView.frame = headerTempFrame;
}

/// 改变footerView的Y值
- (void)footerViewOffsetY:(CGFloat)offsetY {
    CGRect footerTempFrame = self.footerView.frame;
    footerTempFrame.origin.y = offsetY;
    self.footerView.frame = footerTempFrame;
}

- (UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -KHeaderViewH, PW, KHeaderViewH)];
        _headerView.backgroundColor = [UIColor clearColor];
    }
    return _headerView;
}

- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, PH, PW, PH - KHeaderViewH)];
        _footerView.backgroundColor = [UIColor clearColor];
    }
    return _footerView;
}

- (void)addHeaderViewBtnAndLabel{
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"你有FreeStyle吗？";
    lab.textAlignment = NSTextAlignmentCenter;
    lab.textColor = [UIColor whiteColor];
    [self.headerView addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(200);
        make.height.offset(21);
        make.top.equalTo(self.headerView.mas_top).offset(KHeaderViewH * 0.2);
        make.centerX.equalTo(self.headerView.mas_centerX);
    }];
    
    NSArray *arr = @[
                    @"setting_icon",
                    @"github_icon",
                    @"off_line_icon",
                    @"share_icon"];
    
    for (int i = 0; i < arr.count; i++) {
        UIButton *headerViewButton = [[UIButton alloc] init];
        [headerViewButton addTarget:self action:@selector(HeaderViewBtnEvents:) forControlEvents:UIControlEventTouchUpInside];
        headerViewButton.tag = i;
        NSString *imageName = arr[i];
        [headerViewButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [self.headerView addSubview:headerViewButton];
        //添加自动布局   Masonry
        [headerViewButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(38);
            make.height.offset(57);
            make.top.equalTo(self.headerView.mas_top).offset(KHeaderViewH * 0.4);
            make.right.equalTo(self.headerView.mas_left).offset(((PW - 4 * 38) / 5 + 38) * (i + 1));
        }];
    }
}

//
//- (UITableView *)menuTableView {
//
//    if (!self.menuTableView) {
//        _menuTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 190, PH - KHeaderViewH - 80) style:UITableViewStylePlain];
//        _menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _menuTableView.backgroundColor = [UIColor clearColor];
//        _menuTableView.delegate = self;
//        _menuTableView.dataSource = self;
//    }
//    return _menuTableView;
//}

// 黄金三问
#pragma mark -- UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.imageArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellIdentifier = @"menuCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.imageView.image = [UIImage imageNamed:_imageArray[indexPath.row]];
        cell.textLabel.text = self.titleArray[indexPath.row];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 1) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (PH - KHeaderViewH - 80) / _imageArray.count;
}

@end
